package com.yong.ssj.control.admin.sys;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yong.ssj.db.dto.cost.CostMethodDto;
import com.yong.ssj.db.dto.sys.UserDto;
import com.yong.ssj.service.sys.IUserService;
import com.yong.ssj.service.sys.UserServiceImpl;
import com.yong.ssj.util.BeanUtil;

@Controller
@RequestMapping("/admin/sys")
public class UserController {
	@Resource(name="userServiceImpl")
	private IUserService userService;
	
	 @RequestMapping("login")  
	    public String init(){  
	        return "admin/sys/login";   
	    }  
	
	 //登录
	@RequestMapping(value = "login_do", method = { RequestMethod.POST, RequestMethod.GET })
	public String login(@RequestParam String username , @RequestParam String password,ModelMap rps,HttpSession session){
			int i = userService.login(username,password);
			if(i!=0){
				String string=String.valueOf(i);
				session.setAttribute("user", string);
				return "admin/sys/index";
			}else{
				rps.addAttribute("tishi", "账号或密码错误");
				return "admin/sys/login";
			}
	}
	
	@RequestMapping("index")  
    public String index(){  //首页
        return "admin/sys/index";   
    }  
	
	@RequestMapping("quit")  
    public String quit(HttpSession session){  //退出登录
		session.invalidate();
        return "admin/sys/login";   
    }   
	
	@RequestMapping("manger")  
    public String indexhu(ModelMap rps){  //用户管理，先查询所有的客服用户
		List<Map<String, Object>> list = userService.queryUser();
		rps.put("userList", list);
        return "admin/sys/manger";   
    }  
	
	@RequestMapping("cost")  
    public String indexho(ModelMap rps){  //费率
		List<Map<String, Object>> list = userService.queryCost();
		rps.put("costList", list);
        return "admin/sys/cost";   
    }  
	/*public static void main(String[] args) {
		IUserService uus=(IUserService)BeanUtil.getBean("userServiceImpl");
		uus.login("admin", "admin");
	}*/
	@RequestMapping("adduser")  
    public String add(){  //增加管理员页面
        return "admin/sys/adduser";   
    }  
	
	@RequestMapping("adduser_do")  
    public String adduser(UserDto vo,HttpSession session,ModelMap rps){  //增加管理员操作
		long user_id = Long.parseLong((String)session.getAttribute("user"));
		UserDto dto = userService.queryManger(user_id);
		if(dto.getUser_role().equals("1")){
			rps.put("tishi", "您无权增加管理员！");
	        return "admin/sys/adduser"; 
		}else if(dto.getUser_status().equals("0")){
			rps.put("tishi", "您处于冻结状态，无法进行该操作！");
	        return "admin/sys/adduser"; 
		}else{
			UserDto dvo = userService.queryme(vo.getUser_name());
			if(dvo==null){
				int yan = userService.add(vo);
				if(yan==1){
					return "forward:/admin/sys/manger";  
				}else{
					rps.put("tishi", "新增用户失败！");
			        return "admin/sys/adduser";
				}
			}else{
				rps.put("tishi", "您输入的用户名已存在，请重新输入！");
		        return "admin/sys/adduser"; 
			}
		} 
    }  
	
	@RequestMapping("update_manger/{user_id}")  
    public String updateString(ModelMap rps,@PathVariable String user_id){  //修改管理员资料页面
		UserDto dto = userService.queryManger(Long.parseLong(user_id));
		rps.put("UserDto", dto);
        return "admin/sys/update_manger";   
    }  
	
	@RequestMapping("update_manger_do/{user_id}")  
    public String updateMade(UserDto vo,HttpSession session,ModelMap rps,@PathVariable String user_id){  //修改管理员资料操作
		long manger_id = Long.parseLong((String)session.getAttribute("user"));
		UserDto dto = userService.queryManger(manger_id);
		if(dto.getUser_role().equals("1")){
			rps.put("tishi", "您无权修改用户！");
	        return "admin/sys/update_manger"; 
		}else if(dto.getUser_status().equals("0")){
			rps.put("tishi", "您处于冻结状态，无法进行该操作！");
	        return "admin/sys/update_manger"; 
		}else{
			vo.setUser_id(Long.parseLong(user_id));
			int yan = userService.updateUser(vo);
			if(yan==1){
				return "forward:/admin/sys/manger";  
			}else{
				rps.put("tishi", "修改用户失败！");
		        return "admin/sys/update_manger";
			}
		}  
    } 
	
	@RequestMapping("update_cost/{cost_id}")  
    public String updateCost(ModelMap rps,@PathVariable String cost_id){  //修改借款方案页面
		CostMethodDto dto = userService.updateCost(cost_id);
		rps.put("CostMethodDto", dto);
        return "admin/sys/update_cost";   
    }  
	
	@RequestMapping("update_cost_do/{cost_id}")  
    public String updateCostDo(CostMethodDto vo,HttpSession session,ModelMap rps,@PathVariable String cost_id){  //修改借款方案操作
		long manger_id = Long.parseLong((String)session.getAttribute("user"));
		UserDto dto = userService.queryManger(manger_id);
		if(dto.getUser_role().equals("1")){
			rps.put("tishi", "您无权修改方案！");
	        return "admin/sys/update_cost"; 
		}else if(dto.getUser_status().equals("0")){
			rps.put("tishi", "您处于冻结状态，无法进行该操作！");
	        return "admin/sys/update_cost"; 
		}else{
			vo.setCost_id(Long.parseLong(cost_id));
			int yan = userService.updateCostDo(vo);
			if(yan==1){
				return "forward:/admin/sys/cost";  
			}else{
				rps.put("tishi", "修改失败！");
		        return "admin/sys/update_cost";
			}
		}  
    } 
	
	@RequestMapping("addcost")  
    public String addCost(){  //增加方案页面
        return "admin/sys/addcost";   
    }  
	
	@RequestMapping("addcost_do")  
    public String addCostdo(CostMethodDto vo,HttpSession session,ModelMap rps){  //增加管理员操作
		long user_id = Long.parseLong((String)session.getAttribute("user"));
		UserDto dto = userService.queryManger(user_id);
		if(dto.getUser_role().equals("1")){
			rps.put("tishi", "您无权增加方案！");
	        return "admin/sys/addcost"; 
		}else if(dto.getUser_status().equals("0")){
			rps.put("tishi", "您处于冻结状态，无法进行该操作！");
	        return "admin/sys/addcost"; 
		}else{
			userService.addCost(vo);
			return "forward:/admin/sys/cost"; 
		} 
    }  
}
